package com.han.service.impl;

import cn.hutool.json.JSONConverter;
import cn.hutool.json.JSONString;
import com.alibaba.fastjson2.JSONObject;
import com.doudian.open.api.token.AccessTokenData;
import com.han.common.constant.CacheConstants;
import com.han.core.redis.RedisCache;
import com.han.service.AuthService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
public class AuthServiceImpl implements AuthService {
    @Resource
    RedisCache redisCache;

    @Override
    public void setTokenCache(AccessTokenData accessTokenData) {
        String jsonString = JSONObject.toJSONString(accessTokenData);
        //1.存入token
        redisCache.setCacheObject(CacheConstants.TOKEN_PREFIX + accessTokenData.getAccessToken(), accessTokenData, CacheConstants.TOKEN_EXPIRE,TimeUnit.MINUTES);
        //2.存入refresh_token
        redisCache.setCacheObject(CacheConstants.REFRESH_TOKEN_PREFIX + accessTokenData.getRefreshToken(), accessTokenData, CacheConstants.REFRESH_TOKEN_EXPIRE,TimeUnit.MINUTES);
    }
}
